package co.fitstart.api.web.controller.tencent;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.api.entity.CorpMessage;
import co.fitstart.api.handler.TencentHandler;
import co.fitstart.api.web.controller.trading.PaymentController;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/v1")
public class TencentController {

    private static Logger logger = LoggerFactory.getLogger(PaymentController.class);

    @Autowired
    private TencentHandler tencentHandler;

    /**
     * 
     * @param message
     * @return
     */
    @RequestMapping(value = "/send-corp-message", method = RequestMethod.POST)
    public @ResponseBody String onSendCorpMessage(@RequestBody CorpMessage message) {
        Result result = new Result();
        
        try {
            if(StringUtils.isBlank(message.getMobile()) || StringUtils.isBlank(message.getMessage())) {
                result.fail().message("请求参数有误");
            } else {
                result = tencentHandler.sendMessage(message.getMessage(), message.getMobile());
            }
        } catch (Exception e) {
            logger.error("TencentController#onSendCorpMessage() -occur exception: {}", e);
            result.fail().message(e.getMessage());
        }
        
        return JsonUtils.toJsonString(result);
    }
}
